# 将重复的oracle建表语句按照单个表生成对应文件
import re
import os

filePath = r"D:\test\txt"  # input() #文件夹路径
fileEncoding = "GBK"  # 文本编码格式

# 判断生成的表名列表对应的文件夹是否存在
if not os.path.isdir(filePath + "/uu"):
    os.mkdir(filePath + "/uu")
with open(filePath + "\create.sql", "r", encoding=fileEncoding) as fo:
    str = ""
    count = 0
    for fileText in fo:
        if "create table" in fileText:
            if count != 0:  # 当读取到下一个表的时候，将上一个表的内容写入文件
                with open(filePath + "/uu/" + a + ".sql", "w", encoding=fileEncoding) as fo1:
                    fo1.write(str)
                    str = ""
            rep = re.compile("[a-zA-Z]*\.[a-zA-Z\_0-9]*")
            a = "".join(rep.findall(fileText))  # 获取表名
            count = 1
        str += fileText  # 获取内容

    if len(str) > 0:  # 写入最后一张表的信息
        with open(filePath + "/uu/" + a + ".sql", "w", encoding=fileEncoding) as fo1:
            fo1.write(str)
            str = ""
